<?php

class TimePicker extends TTemplateControl
{
	public $time;
	
	public function onLoad()
	{
		if(!$this->Page->IsPostback)
		{
			if(!isset($this->time))
				$this->time = DateTimeParser::getCurrentDateTime();
			$this->construct();
			$this->readTime();
		}
	}
	
	public function construct()
	{
		$hour_list_array = array();
		for($i = 0; $i < 24; $i++)
		{
			$title = $i < 10 ? '0'.$i : $i;
			$hour_list_array[] = array('id' => $i, 'title' => $title);
		}
		$this->hour_list->DataValueField = 'id';
		$this->hour_list->DataTextField  = 'title';
		$this->hour_list->DataSource = $hour_list_array;
		$this->hour_list->dataBind();
		
		$minute_list_array = array();
		for($i = 0; $i < 60; $i++)
		{
			$title = $i < 10 ? '0'.$i : $i;
			$minute_list_array[] = array('id' => $i, 'title' => $title);
		}
		$this->minute_list->DataValueField = 'id';
		$this->minute_list->DataTextField  = 'title';
		$this->minute_list->DataSource = $minute_list_array;
		$this->minute_list->dataBind();
		
		$second_list_array = array();
		for($i = 0; $i < 60; $i++)
		{
			$title = $i < 10 ? '0'.$i : $i;
			$second_list_array[] = array('id' => $i, 'title' => $title);
		}
		$this->second_list->DataValueField = 'id';
		$this->second_list->DataTextField  = 'title';
		$this->second_list->DataSource = $second_list_array;
		$this->second_list->dataBind();
	}
	
	public function readTime()
	{
		$parser = new DateTimeParser($this->time);	
		$hour = $parser->getHour();
		$minute = $parser->getMinute();
		$second = $parser->getSecond();
		
		$this->hour_list->setSelectedValue($hour);
		$this->minute_list->setSelectedValue($minute);
		$this->second_list->setSelectedValue($second);
	}
	
	public function getHour()
	{
		return $this->hour_list->getSelectedValue();
	}
	
	public function getMinute()
	{
		return $this->minute_list->getSelectedValue();
	}
	
	public function getSecond()
	{
		return $this->second_list->getSelectedValue();
	}
	
	public function getTime()
	{
		$second = '0';
		if($this->getSecondVisible())
			$second = $this->getSecond();
		
		$hour = $this->getHour();
		if($hour < 10) $hour = '0'.$hour;
		
		$minute = $this->getMinute();
		if($minute < 10) $minute = '0'.$minute;
		
		if($hour < 10) $second = '0'.$second;
			
		return $hour.':'.$minute.':'.$second;
	}
	
	public function setTime($time)
	{
		$this->time = $time;
	}
	
	// enablig seconds
	public function getSecondVisible()
	{
		return $this->getViewState('second_visible', false);
	}
	
	public function setSecondVisible($visible)
	{
		return $this->setViewState('second_visible', $visible, false);
	}
}

?>